package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MaintenancePlan;
import com.ruoyi.system.service.IMaintenancePlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维护计划Controller
 * 
 * @author ruoyi
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/system/plan")
public class MaintenancePlanController extends BaseController
{
    @Autowired
    private IMaintenancePlanService maintenancePlanService;

    /**
     * 查询维护计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaintenancePlan maintenancePlan)
    {
        startPage();
        List<MaintenancePlan> list = maintenancePlanService.selectMaintenancePlanList(maintenancePlan);
        return getDataTable(list);
    }

    /**
     * 导出维护计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:export')")
    @Log(title = "维护计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaintenancePlan maintenancePlan)
    {
        List<MaintenancePlan> list = maintenancePlanService.selectMaintenancePlanList(maintenancePlan);
        ExcelUtil<MaintenancePlan> util = new ExcelUtil<MaintenancePlan>(MaintenancePlan.class);
        util.exportExcel(response, list, "维护计划数据");
    }

    /**
     * 获取维护计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plan:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(maintenancePlanService.selectMaintenancePlanByDeviceId(deviceId));
    }

    /**
     * 新增维护计划
     */
    @PreAuthorize("@ss.hasPermi('system:plan:add')")
    @Log(title = "维护计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaintenancePlan maintenancePlan)
    {
        return toAjax(maintenancePlanService.insertMaintenancePlan(maintenancePlan));
    }

    /**
     * 修改维护计划
     */
    @PreAuthorize("@ss.hasPermi('system:plan:edit')")
    @Log(title = "维护计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaintenancePlan maintenancePlan)
    {
        return toAjax(maintenancePlanService.updateMaintenancePlan(maintenancePlan));
    }

    /**
     * 删除维护计划
     */
    @PreAuthorize("@ss.hasPermi('system:plan:remove')")
    @Log(title = "维护计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(maintenancePlanService.deleteMaintenancePlanByDeviceIds(deviceIds));
    }

    /**
     * 根据区域ID查询维护计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:list')")
    @GetMapping("/listByAreaId/{areaId}")
    public TableDataInfo listByAreaId(@PathVariable("areaId") Long areaId)
    {
        startPage();
        List<MaintenancePlan> list = maintenancePlanService.selectMaintenancePlanByAreaId(areaId);
        return getDataTable(list);
    }

    /**
     * 根据矿工ID查询维护计划列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:list')")
    @GetMapping("/listByMineId/{mineId}")
    public TableDataInfo listByMineId(@PathVariable("mineId") Long mineId)
    {
        startPage();
        List<MaintenancePlan> list = maintenancePlanService.selectMaintenancePlanByMineId(mineId);
        return getDataTable(list);
    }
}
